/**
 * 
 */
package view.impl.menu.items;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.geom.RoundedRectangle;


/**
 * @author Julian
 *
 */
public class MenuTextBox implements IMenuItem {

	private static float scaleX = 1;
	private static float scaleY = 1;
	
	private IMenuArrow arrows;
	private String textBefore;
	private String textAfter;
	private int posX;
	private int posY;
	private UnicodeFont textFont;			// Text on the controller.
	private UnicodeFont userTextFont;		// Text entered by user.
	private Color fontColor;
	private Color userFontColor;
	private int maxChars;					// Maximum allowed entered characters.
	
	private String userText;
	
	private int charWidth;
	
	public MenuTextBox(String textBefore, 
				String textAfter,
				UnicodeFont textFont, 
				UnicodeFont userTextFont, 
				int maxChars) {
		this(textBefore, textAfter, 0, 0, textFont, userTextFont, maxChars);
	}
	
	public MenuTextBox(String textBefore, String textAfter, int posX, int posY, UnicodeFont staticTextFont, UnicodeFont dynTextFont, int maxChars) {
		this(new MenuArrow(posX,
						   posY,
						   (int) (staticTextFont.getWidth(textBefore + textAfter) + 	// Get the width.
								  dynTextFont.getWidth("a") * maxChars + 20 * scaleX),
						   staticTextFont.getHeight(textBefore)),
			 textBefore,
			 textAfter,
			 "",
			 staticTextFont,
			 dynTextFont,
			 maxChars,
			 new Color(255, 185, 25),
			 Color.black);
	}
	
	public MenuTextBox(IMenuArrow arrows, 
					   String textBefore, 
					   String textAfter,
					   String userText,
					   UnicodeFont staticTextFont, 
					   UnicodeFont dynTextFont, 
					   int maxChars, 
					   Color fontColor, 
					   Color userFontColor) {
		this.arrows = arrows;
		this.textBefore = textBefore;
		this.textAfter = textAfter;
		this.userText = userText;
		this.textFont = staticTextFont;
		this.userTextFont = dynTextFont;
		this.maxChars = maxChars;
		this.charWidth = userTextFont.getWidth("a");
		this.fontColor = fontColor;
		this.userFontColor = userFontColor;
	}
	
	public void setUserText(String txt) {
		this.userText = txt;
	}
	
	/*
	public void setCursorPos(int pos) {
		this.curPos = pos;
	}
	*/
	
	/* (non-Javadoc)
	 * @see client.view.IMenuItem#draw(boolean)
	 */
	@Override
	public void draw(boolean selected, Graphics g, int windowWidth, int windowHeight) {
		// Init values.
		float averageScale = (scaleX + scaleY) / 2;
		
		int textBeforeWidth = textFont.getWidth(textBefore);
		int textAfterWidth = textFont.getWidth(textAfter);

		if(userText == null) {
			userText = " ";
		}
		int userTextHeight = (int) (userTextFont.getHeight(userText) + 6 * scaleY);
		int userTextWidth = (int) (userTextFont.getWidth(userText) + 16 * scaleX);
		
		int centeredPosY = posY + textFont.getHeight(textBefore) / 2;
		
		
		// Draw stuff.
		
		// Draw text before.
		textFont.drawString(posX, posY, textBefore, fontColor);
		
		// Draw the box.
		RoundedRectangle box = new RoundedRectangle(posX + textBeforeWidth + 5 * scaleX, 
													centeredPosY - userTextHeight / 2, 
													charWidth * maxChars + 6 * scaleX, 
													userTextHeight, 
													3 * averageScale);
		
		g.setAntiAlias(true);
		g.setColor(Color.white);
		g.fill(box);
		g.setLineWidth(3 * averageScale);
		g.setColor(Color.cyan);
		g.draw(box);
		g.setLineWidth(1);
		
		// Draw user text.
		int userTextPosX = (int) (posX + textBeforeWidth + 8 * scaleX);
		int userTextPosY = centeredPosY - userTextHeight / 2;
		
		userTextFont.drawString(userTextPosX, userTextPosY, userText, userFontColor);
		
		// Draw text after.
		textFont.drawString(posX + textAfterWidth + userTextWidth, posY, textAfter, fontColor);
		
		if(selected) {
			// Draw arrows.
			arrows.draw(g, windowWidth, windowHeight);
		}
	}

	/* (non-Javadoc)
	 * @see client.view.IMenuItem#getHeight()
	 */
	@Override
	public int getHeight() {
		int boxHeight = (int) (userTextFont.getHeight(userText) + 6 * scaleY);
		if(textFont.getHeight(textBefore) > boxHeight) {
			return textFont.getHeight(textBefore);
		}
		else {
			return boxHeight;
		}
	}

	/* (non-Javadoc)
	 * @see client.view.IMenuItem#getPosX()
	 */
	@Override
	public int getPosX() {
		return posX;
	}

	/* (non-Javadoc)
	 * @see client.view.IMenuItem#getPosY()
	 */
	@Override
	public int getPosY() {
		return posY;
	}

	/* (non-Javadoc)
	 * @see client.view.IMenuItem#getWidth()
	 */
	@Override
	public int getWidth() {
		return (int) (textFont.getWidth(textBefore + textAfter) + charWidth * maxChars + 16 * scaleX);
	}

	/* (non-Javadoc)
	 * @see client.view.IMenuItem#move(int, int)
	 */
	@Override
	public void move(int deltaX, int deltaY) {
		setPos(posX + deltaX, posY + deltaY);
	}

	/* (non-Javadoc)
	 * @see client.view.IMenuItem#setPos(int, int)
	 */
	@Override
	public void setPos(int posX, int posY) {
		this.posX = posX;
		this.posY = posY;
		arrows.setPosition(posX, posY);
	}

}
